﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Yes.Core.Helper

{
    public class MailHelper
    {
        public string EamilFrom { get; set; }
        public string EamilUser { get; set; }
        public string EmailTo { get; set; }
        public string EmailTitle { get; set; }
        public string EmailContent { get; set; }
        public string EamilSmtp { get; set; }
        public int EmailPort { get; set; }
        public string EamilPwd { get; set; }
        public Encoding Encoding { get; set; }
        public string ErrorMessage { get ; set; }

        public bool Send()
        {
            MailMessage message = new MailMessage(
            new MailAddress(EamilFrom, EamilUser, Encoding),
            new MailAddress(EmailTo));
            message.SubjectEncoding = Encoding;
            message.Subject = EmailTitle;
            message.BodyEncoding = Encoding;
            message.IsBodyHtml = true;
            message.Body = EmailContent;
            SmtpClient smtpClient = new SmtpClient(EamilSmtp, EmailPort);
            smtpClient.Credentials = new NetworkCredential(EamilFrom, EamilPwd);
            //smtpClient.Timeout = 999999;
            try
            {
                smtpClient.Send(message);
                return true;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                return false;
            }
        }
    }
}
